import 'package:flutter/material.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:markdown_widget/markdown_widget.dart';
import '../../style/app_colors.dart';
import '../../style/widget_style.dart';
import '../controller/RecordTransciptSummeryController.dart';

class Record2scriptSumerise extends GetView<RecordTransciptSummeryController> {
  String searchTerm = "Flutter";

  Record2scriptSumerise({super.key});



  @override
  Widget build(BuildContext context) {
    // _controller. = controller.summaryContent.value;
    // 固定在屏幕底部中央
    final screenSize = MediaQuery.of(context).size;
    final primaryAnchor = Offset(screenSize.width / 2, screenSize.height - 100);

    return Scaffold(
      backgroundColor: AppColors.scaffold_background_color,
      body:  Obx(()=> controller.summaryContent.value != '' ? Container(
        padding: EdgeInsets.all(15.w),
        child: Column(
          children: [
            controller.editorModition.value ? Container(
              width: Get.width,
              child: Scrollbar(
                  controller: controller.singleChildController, // 关联控制器
                  thumbVisibility: true, // 始终显示滚动条（可选）
                  thickness: 3.0, // 滚动条宽度
                  child: SingleChildScrollView(
                    controller: controller.singleChildController,
                    child: Column(
                      children: [
                        QuillSimpleToolbar(
                          controller: controller.quillController,

                          config: QuillSimpleToolbarConfig(
                              showSearchButton: false,
                              multiRowsDisplay: false,
                              showLink: false,
                              showClipboardCut: true,
                              showClipboardCopy: true,
                              showClipboardPaste: true,
                              color: AppColors.scaffold_background_color,
                              /*iconTheme: QuillIconTheme(
                                iconButtonSelectedData: IconButtonData(
                                    style: ButtonStyle(
                                      backgroundColor: WidgetStateProperty.resolveWith((s) {
                                        return  Colors.blue;
                                      })
                                    )
                                ), // 选中时图标颜色
                                iconButtonUnselectedData: IconButtonData(
                                    style: ButtonStyle(
                                        backgroundColor: WidgetStateProperty.resolveWith((s) {
                                          return  Colors.white;
                                        })
                                    )
                                ), // 选中时背景色
                              ),*/


                          ),

                        ),
                        SizedBox(height: 10.h,),
                        /*Row(
                          children: [
                            InkWell(
                              onTap: () {
                                controller.saveModification();
                              },
                              child: Container(
                                margin: EdgeInsets.only(left: 25.w,right: 25.w),
                                child: Column(
                                  children: [
                                    Image.asset('assets/images/icon_script_bottom_menu_save.png',width: 16.w,height: 16.h,),
                                    SizedBox(height: 6.h),
                                    Text('保存')
                                  ],
                                ),
                              ),
                            ),
                            InkWell(
                              onTap: () {
                                controller.cancelModification();
                              },
                              child: Container(
                                margin: EdgeInsets.only(left: 25.w,right: 25.w),
                                child: Column(
                                  children: [
                                    Image.asset('assets/images/icon_menu_moving_folder.png',width: 16.w,height: 16.h,),
                                    SizedBox(height: 6.h),
                                    Text('取消')
                                  ],
                                ),
                              ),
                            )
                          ],
                        )*/
                      ],
                    ),
                  )
              ),
            ): SizedBox(),
            Expanded(
              child: QuillEditor.basic(
                controller: controller.quillController,
                focusNode: controller.focusNode,
                config:  QuillEditorConfig(
                    placeholder: '输入内容',
                    contextMenuBuilder: (context, state) {
                      List<ContextMenuButtonItem> contextButton = state.contextMenuButtonItems;

                      if (!controller.editorModition.value) {
                        ContextMenuButtonItem modificationBtn = ContextMenuButtonItem(label: '编辑',onPressed: () {
                          controller.changeModification();
                        }, type: ContextMenuButtonType.custom);
                        contextButton.add(modificationBtn);
                      }

                      return TextFieldTapRegion(
                        child: AdaptiveTextSelectionToolbar.buttonItems(
                          buttonItems: contextButton,
                          anchors: state.contextMenuAnchors,
                        ),
                      );
                    },
                    enableSelectionToolbar: controller.enableSelectionToolbar.value
                ),

              ),
            )
          ],
        ),
      ): Column(
        children: [
          Container(
            padding: EdgeInsets.all(18.w),
            margin: EdgeInsets.all(18.h),
            decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.all(Radius.circular(20)),
                color: Colors.white
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        Container(
                          child: Stack(
                            children: [
                              Container(
                                margin: EdgeInsets.only(left: 9.w,top: 9.h),
                                child: Image.asset('assets/images/icon_sumery_noting.png',width: 21.w,height: 26.h),
                              ),
                              Image.asset('assets/images/icon_summery_star_small.png',width: 9.w,height: 9.h),

                            ],
                          ),
                        ),
                        SizedBox(width: 8.w,),
                        Container(
                          margin: EdgeInsets.only(top: 15.h),
                          child: Text('会议记录',style: AppStyleThemeData.PAGE_TITLE,),
                        )
                      ],
                    ),
                    Image.asset('assets/images/icon_summery_start_big.png',width: 18.w,height:18.h),

                  ],
                ),
                Container(
                  margin: EdgeInsets.only(left: 38.w,top: 10.h),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('1.上个月整体销售额大幅增长30%',style: AppStyleThemeData.HINT_TEXT_STYLE_medium,),
                      SizedBox(height: 5.h,),
                      Text('2.推广活动初步取得成效,用户增长20%',style: AppStyleThemeData.HINT_TEXT_STYLE_medium,),
                      SizedBox(height: 5.h,),
                      Text('3.产品需要进一步优化',style: AppStyleThemeData.HINT_TEXT_STYLE_medium,)
                    ],
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        Container(
                          margin: EdgeInsets.only(top: 15.h,left: 15.w),
                          child: Image.asset('assets/images/icon_sumery_canlender.png',width: 21.w,height: 26.h),
                        ),
                        SizedBox(width: 8.w,),
                        Container(
                          margin: EdgeInsets.only(top: 15.h),
                          child: Text('下一步计划',style: AppStyleThemeData.PAGE_TITLE,),
                        )
                      ],
                    )
                  ],
                ),
                Container(
                  margin: EdgeInsets.only(left: 38.w,top: 10.h),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('1.与供应链确认生产计划',style: AppStyleThemeData.HINT_TEXT_STYLE_medium,),
                      SizedBox(height: 5.h,),
                      Text('2.完善销售方案',style: AppStyleThemeData.HINT_TEXT_STYLE_medium,),
                      SizedBox(height: 5.h,),
                      Text('3.研发改进用户提现.',style: AppStyleThemeData.HINT_TEXT_STYLE_medium,),
                      SizedBox(height: 15.h,),
                      Text('.....',style: AppStyleThemeData.HINT_TEXT_STYLE_medium,)
                    ],
                  ),
                ),

              ],
            ),
          ),
          SizedBox(height: 40.h),
          ElevatedButton(child:  Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(width: 46.w,),
              Image.asset('assets/images/icon_summery_generate_btn.png',width: 24.w,height: 24.h,),
              SizedBox(width: 11.w,),
              Text('生成'),
              SizedBox(width: 46.w,),
            ],
          ),onPressed: () {
              controller.generateSummery();

          })
        ],
      )),
    );
  }

}




class HighlightTextGenerator {
  final String searchTerm;
  final TextStyle highlightStyle;

  HighlightTextGenerator({
    required this.searchTerm,
    required this.highlightStyle,
  });

  InlineSpan generate(TextNode node) {
    final text = node.text;
    if (searchTerm.isEmpty) {
      return TextSpan(text: text, style: node.style);
    }

    final spans = <InlineSpan>[];
    int lastIndex = 0;

    // 不区分大小写匹配
    final regex = RegExp(searchTerm, caseSensitive: false);
    final matches = regex.allMatches(text);

    for (final match in matches) {
      // 添加匹配前的普通文本
      if (match.start > lastIndex) {
        spans.add(TextSpan(
          text: text.substring(lastIndex, match.start),
          style: node.style,
        ));
      }

      // 添加高亮文本
      spans.add(TextSpan(
        text: text.substring(match.start, match.end),
        style: node.style?.merge(highlightStyle) ?? highlightStyle,
      ));

      lastIndex = match.end;
    }

    // 添加剩余文本
    if (lastIndex < text.length) {
      spans.add(TextSpan(
        text: text.substring(lastIndex),
        style: node.style,
      ));
    }

    return TextSpan(children: spans);
  }
}
